package code_generator;

import java.io.File;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class GUI extends JPanel {
	private JLabel xmlFileLabel;
	private JPanel contentPane;
	private JTextArea textArea;
	private JScrollPane scrollPane;
	private String[] langList;
	private JComboBox langComboBox;
	private JButton generateButton;
	private JPanel actionPanel;
        
        // PATH FOR XML FILE TO BE READ OF DCD
        // private final String XML_FILE = "C:"+File.separator+"Test"+File.separator+"DCD_Test.xml";
	private final String XML_FILE = "DCD.xml";
	
	public GUI() {
		this.setLayout(new BorderLayout());
		JPanel contentPane = new JPanel(new BorderLayout());
		
		JTextArea textArea = new JTextArea(1, 30);
		JScrollPane scrollPane = new JScrollPane(textArea);
		
		String[] langList = {"Java"};
		JComboBox langComboBox = new JComboBox(langList);

		try {
			xmlFileLabel = new JLabel(new File(".").getCanonicalPath() + File.separator + XML_FILE);
		}
		catch(Exception e) {
			xmlFileLabel = new JLabel("Reading from DCD.xml");
		}
		
		JButton generateButton = new JButton();
		generateButton.setText("Generate Code");
		generateButton.addActionListener(new generateListener(textArea));
		
		JPanel actionPanel = new JPanel();
		actionPanel.setLayout(new BoxLayout(actionPanel, BoxLayout.LINE_AXIS));
		actionPanel.add(langComboBox);
		actionPanel.add(Box.createHorizontalStrut(15));
		actionPanel.add(generateButton);
		
		contentPane.add(actionPanel, BorderLayout.SOUTH);
                this.add(contentPane, BorderLayout.CENTER);
	}
	
	private class generateListener implements ActionListener {
		private JTextArea textArea;
		public void actionPerformed(ActionEvent e) {
			textArea.setText(XML_FILE);
                        CodeGenController controller = new CodeGenController();
                        //FILE PATH FOR XML
                        boolean success = controller.generate(XML_FILE);
			
			try {

				String path = new File(".").getCanonicalPath();
				if(success)
				{
				    JOptionPane.showMessageDialog(null, "Your code was successfully generated at " +
					    path + File.separator + "Output Code");
				}
				else
				{
				    JOptionPane.showMessageDialog(null, "Error: your code could not be generated");
				}
			}
			catch(Exception ex) {
				System.err.println(ex.getMessage());
			}
			
		}
		
		public generateListener(JTextArea textArea) {
			this.textArea = textArea;
		}
	}

}
